home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Apple II Sample Code / APW.SC / SC23SoundEx / SoundEx.p < prev    next >
Encoding:
Text File  |  1990-06-24  |  19.5 KB  |  654 lines  |  [TEXT/pdos]

  1. {
  2. *   Standard Application Shell - Pascal Version - Modified for SoundEx
  3. *
  4. *       v3.0    Luther, Roberts
  5. }
  6.  
  7. {
  8. *
  9. * Developer Technical Support Apple II Sample Code
  10. *
  11. *    Copyright (c) Apple Computer, Inc. 1989-1990
  12. *               All Rights Reserved
  13. **
  14. *   This program and its derivatives are licensed only for
  15. *   use on Apple computers.
  16. *
  17. *   Works based on this program must contain and
  18. *   conspicuously display this notice.
  19. *
  20. *   This software is provided for your evaluation and to
  21. *   assist you in developing software for the Apple IIGS
  22. *   computer.
  23. *
  24. *   DISCLAIMER OF WARRANTY
  25. *
  26. *   THE SOFTWARE IS PROVIDED "AS IS" WITHOUT
  27. *   WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  28. *   WITH RESPECT TO ITS MERCHANTABILITY OR ITS FITNESS
  29. *   FOR ANY PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO
  30. *   THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
  31. *   YOU.  SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU (AND
  32. *   NOT APPLE OR AN APPLE AUTHORIZED REPRESENTATIVE)
  33. *   ASSUME THE ENTIRE COST OF ALL NECESSARY SERVICING,
  34. *   REPAIR OR CORRECTION.
  35. *
  36. *   Apple does not warrant that the functions
  37. *   contained in the Software will meet your requirements
  38. *   or that the operation of the Software will be
  39. *   uninterrupted or error free or that defects in the
  40. *   Software will be corrected.
  41. *
  42. *   SOME STATES DO NOT ALLOW THE EXCLUSION
  43. *   OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY
  44. *   NOT APPLY TO YOU.  THIS WARRANTY GIVES YOU SPECIFIC
  45. *   LEGAL RIGHTS AND YOU MAY ALSO HAVE OTHER RIGHTS
  46. *   WHICH VARY FROM STATE TO STATE.
  47. }
  48.  
  49.  
  50. program Shell;
  51.  
  52. uses
  53.     Types,
  54.     GSOS,
  55.     Locator,
  56.     ADB,
  57.     IntMath,
  58.     TextTool,
  59.     Memory,
  60.     SANE,
  61.     ACE,
  62.     Resources,
  63.     MiscTool,
  64.     Scheduler,
  65.     Loader,
  66.     Quickdraw,
  67.     QDAux,
  68.     Events,
  69.     Controls,
  70.     Windows,
  71.     Menus,
  72.     LineEdit,
  73.     Dialogs,
  74.     Sound,
  75.     NoteSyn,
  76.     NoteSeq,
  77.     MIDI,
  78.     StdFile,
  79.     Scrap,
  80.     Desk,
  81.     Lists,
  82.     Fonts,
  83.     Print,
  84.     TextEdit;
  85.  
  86.  
  87. { ParamRec contains the ParamText strings used in the static text controls. }
  88.  
  89. type ParamRec = RECORD
  90.                     StrPtr  : array[0..9] of String255Ptr;
  91.                     Str     : array[0..9] of String255;
  92.                 END;
  93.  
  94. { CtlHiliteRec keeps track of which LineEdit controls are active.
  95.   SoundEx will toggle between a LineEdit control and a static text
  96.   control depending on whether the control is active or not,
  97.   respectively }
  98.   
  99.      CtlHiliteRec = RECORD
  100.                         docBlk : Boolean;
  101.                         sndBlk : Boolean;
  102.                         parm   : array[1..3] of Boolean;
  103.                     END;
  104.  
  105. const   
  106.     MainWindowID = $1000;
  107.     
  108.     { menu item numbers for standard DA menu items }
  109.     UndoID      = 250;
  110.     CutID       = 251;
  111.     CopyID      = 252;
  112.     PasteID     = 253;
  113.     ClearID     = 254;
  114.     CloseID     = 255;
  115.  
  116.     { menu numbers }
  117.     AppleMenuID     = $1100;    { 1st menu of 1st menu bar }
  118.     FileMenuID      = $1200;    { 2nd menu of 1st menu bar }
  119.     EditMenuID      = $1300;    { 3rd menu of 1st menu bar }
  120.     SoundMenuID     = $1400;    { 4th menu of 1st menu bar }
  121.  
  122.     { application menu item numbers }
  123.     AboutID         = AppleMenuID + 1;  { 1st item of 1st menu of 1st menu bar }
  124.     QuitID          = FileMenuID  + 2;  { 2nd item of 2nd menu of 1st menu bar }
  125.  
  126.     { Sound pop-up menu item numbers. }
  127.     SoundVersionID      = SoundMenuID + 4;
  128.     SoundToolStatusID   = SoundMenuID + 6;
  129.     FFGeneratorStatusID = SoundMenuID + 7;
  130.     FFSoundDoneStatusID = SoundMenuID + 8;
  131.     FFSoundStatusID     = SoundMenuID + 9;
  132.     FFStartSoundID      = SoundMenuID + 10;
  133.     FFStopSoundID       = SoundMenuID + 11;
  134.     GetSoundVolumeID    = SoundMenuID + 12;
  135.     GetTableAddressID   = SoundMenuID + 13;
  136.     ReadRamBlockID      = SoundMenuID + 14;
  137.     SetSoundMIRQVID     = SoundMenuID + 15;
  138.     SetSoundVolumeID    = SoundMenuID + 16;
  139.     SetUserSoundIRQVID  = SoundMenuID + 17;
  140.     WriteRamBlockID     = SoundMenuID + 18;
  141.     FFSetUpSoundID      = SoundMenuID + 19;
  142.     FFStartPlayingID    = SoundMenuID + 20;
  143.     SetDOCRegID         = SoundMenuID + 21;
  144.     ReadDOCRegID        = SoundMenuID + 22;
  145.  
  146.     { staticText ID's which use paramText } 
  147.     curfileID           = 228;
  148.     loadedAtID          = 229;
  149.     buffSizeID          = 230;
  150.     resultID            = 231;
  151.     toolerrID           = 232;
  152.  
  153.     { LineEdit control ID's }
  154.     oscGenTypeID        = 301;
  155.     freqLow1ID          = 302;
  156.     freqHigh1ID         = 303;
  157.     vol1ID              = 304;
  158.     tablePtr1ID         = 305;
  159.     control1ID          = 306;
  160.     tableSize1ID        = 307;
  161.     freqLow2ID          = 308;
  162.     freqHigh2ID         = 309;
  163.     vol2ID              = 310;
  164.     tablePtr2ID         = 311;
  165.     control2ID          = 312;
  166.     tableSize2ID        = 313;
  167.     waveStartID         = 314;
  168.     waveSizeID          = 315;
  169.     freqOffsetID        = 316;
  170.     docBufferID         = 317;
  171.     bufferSizeID        = 318;
  172.     nextWavePtrID       = 319;
  173.     volSettingID        = 320;
  174.     Parm1ID             = 321;
  175.     Parm2ID             = 322;
  176.     Parm3ID             = 323;
  177.     
  178.     execbut             = 350;
  179.     loadbut             = 351;
  180.     sndCmdPopUp         = 352;
  181.  
  182.     MyTaskMask      = $001FFFFF;{ handle all events possible }
  183.  
  184.     { resource ID numbers }
  185.     BaseResID       = $00000000;    { start of resource ID numbers }
  186.     MenuBarOneRID   = $00001000;    { resource ID of menu bar }
  187.         
  188. var
  189.     { Standard global variables here }
  190.     MyMemoryID      : integer;  { application's memory ID }
  191.     Done            : boolean;  { flag to show when to quit application }
  192.     ToolRecRef      : Ref;      { StartStopRecRef from StartUpTools }
  193.     WindowKind      : integer;  { type of top window from GetWKind call }
  194.     MenuHeight      : integer;  { stored height of menu bar }
  195.     OurWindow       : WindowPtr;
  196.     
  197.     {   The following is the record that is used by TaskMaster to return
  198.         events. It is similar to a regular event record, except that there are
  199.         two additional fields at the end. The first is used to convey some
  200.         TaskMaster specific data back to the application. The second is called
  201.         the TaskMask and is used to tell TaskMaster what situations to handle.
  202.         In this shell, we tell TaskMaster to handle everything by setting all
  203.         currently defined bits to 1 (MyTaskMask) in the initApp procedure.  }
  204.  
  205.     MyEvent         : wmTaskRec;
  206.     
  207.     paramRec1       : ParamRec;
  208.     
  209.     BuffHndl        : Handle;
  210.     BuffSize        : integer;
  211.  
  212.     CurCmdNum       : Integer;  {Current Menu Item (sound command) selected}
  213.     CurCmdErr       : Integer;
  214.     CurCmdResult    : Longint;
  215.     ctlHilite       : CtlHiliteRec;
  216.     parmArray       : packed array[oscGenTypeID..parm3ID] of String[8];
  217.     
  218. {$I SoundEx.inc.p}
  219.  
  220. {******************************************************************************
  221. *
  222. * errorCheck:   This procedure is declared forward. This lets you check for
  223. *               fatal errors and still shut down fairly cleanly from
  224. *               anywhere in your program.
  225. }
  226.  
  227. procedure errorCheck(where : Integer);
  228.     FORWARD;
  229.     
  230.     
  231. {******************************************************************************
  232. *
  233. * doQuit:       Set the Done flag to true. This tells the Event loop to exit.
  234. *
  235. * Inputs:       NONE
  236. * Outputs:      NONE
  237. * Calls:        NONE
  238. }
  239.  
  240. procedure doQuit;
  241.  
  242. begin
  243.     Done := true;
  244. end;
  245.  
  246.  
  247. {******************************************************************************
  248. *
  249. * doAbout:      Bring up an Alert Dialog box with our name in it.
  250. *
  251. * Inputs:       NONE
  252. * Outputs:      NONE
  253. * Calls:        NONE
  254. }
  255.  
  256. procedure doAbout;
  257.  
  258. const
  259.     alertFlags  = 4;        { reference is a ResourceID }
  260.  
  261. var
  262.     buttonHit   : integer;      { button number clicked }
  263.  
  264. begin
  265.     buttonHit := AlertWindow(alertFlags,NIL,Pointer(BaseResID+1));
  266. end;
  267.  
  268. {******************************************************************************
  269. *
  270. * doMenu:       This routine is called when TaskMaster returns a menu
  271. *               event. It takes the menu item that was hit and calculates
  272. *               an offset into the menu dispatch table. It then calls that
  273. *               routine and unhilites the menu when it is done. 
  274. *
  275. * Inputs:       TaskData holds menu item selected.
  276. * Outputs:      NONE
  277. * Calls:        doAbout, doQuit
  278. }
  279.  
  280. procedure doMenu;
  281.     
  282. var
  283.     menuNum,                    { ID of menu from which selection was made }
  284.     itemNum     : integer;      { ID of selected menu item }
  285.     
  286. begin
  287.     menuNum := HiWord(MyEvent.wmTaskData);  { get menu ID }
  288.     itemNum := LoWord(MyEvent.wmTaskData);  { and item ID from MyEvent}
  289.     
  290.     case itemNum of
  291.         AboutID             : doAbout;  { show About alert }
  292.         QuitID              : doQuit;   { set Done flag }
  293.         UndoID              :;
  294.         CutID               :;
  295.         CopyID              :;
  296.         PasteID             :;
  297.         ClearID             :;
  298.     end;
  299.     
  300.         {   The routine has been called. Unhilite the menu and return to the
  301.         Main Event Loop. }
  302.  
  303.     HiLiteMenu(false,menuNum);
  304. end;
  305.  
  306. {******************************************************************************
  307. *
  308. * doSysChange:  Called by testTopWindow when the active window
  309. *               has changed to or from a system window.
  310. *
  311. * Inputs:       Bit 15 of WindowKind is 0 if top window is an application
  312. *               window, 1 if top window is a system window.
  313. * Outputs:      NONE
  314. * Calls:        NONE
  315. }
  316.  
  317. procedure doSysChange;
  318.  
  319. begin
  320.     if Band(WindowKind,$8000) <> 0 { if bit 15 of WindowKind = 1, MPW Pascal call! }
  321.         then
  322.             begin
  323.                 { enable the edit menu items and the close item }
  324.                 EnableMItem(UndoID);
  325.                 EnableMItem(CutID);
  326.                 EnableMItem(CopyID);
  327.                 EnableMItem(PasteID);
  328.                 EnableMItem(ClearID);
  329.                 EnableMItem(CloseID);
  330.                 
  331.                 { if your edit menu has items that are selectable when a
  332.                   NDA is not the active window, remove the next two lines. }
  333.                 SetMenuFlag(enableMenu,EditMenuID);
  334.                 DrawMenuBar;
  335.             end
  336.         else
  337.             begin
  338.                 { disable the edit menu items and the close item }
  339.                 DisableMItem(UndoID);
  340.                 DisableMItem(CutID);                
  341.                 DisableMItem(CopyID);               
  342.                 DisableMItem(PasteID);              
  343.                 DisableMItem(ClearID);              
  344.                 DisableMItem(CloseID);
  345.                 
  346.                 { if your edit menu has items that are selectable when a
  347.                   NDA is not the active window, remove the next two lines. }
  348.                 SetMenuFlag(disableMenu,EditMenuID);
  349.                 DrawMenuBar;                
  350.             end;
  351. end;
  352.  
  353.  
  354. {******************************************************************************
  355. *
  356. * testTopWindow:This routine is called on every time through the event loop.
  357. *               If the type to the top window has changed from application
  358. *               window to system window or back, this routine will call
  359. *               doSysChange.
  360. *
  361. * Inputs:       NONE
  362. * Outputs:      NONE
  363. * Calls:        doSysChange
  364. }
  365.  
  366. procedure testTopWindow;
  367.  
  368. var
  369.     tempWindowPtr   : WindowPtr;    { active window's grafPort }
  370.     tempWindowKind  : integer;  { active window's kind }
  371.  
  372. begin
  373.     tempWindowPtr := FrontWindow;   { get active window's grafPort }
  374.     
  375.     if tempWindowPtr <> NIL     { if there is an active window }
  376.         then tempWindowKind := GetWKind(tempWindowPtr) { get its kind }
  377.         else tempWindowKind := 0; { force to application window kind }
  378.         
  379.     if tempWindowKind <> WindowKind 
  380.         then                    { window kind has changed }
  381.             begin               { save the WindowKind and change the menus }
  382.                 WindowKind := tempWindowKind;
  383.                 doSysChange;
  384.             end;
  385. end;
  386.  
  387.  
  388. procedure DrawThisWindow;
  389.     begin
  390.         WaitCursor;
  391.         DrawControls(GetPort);
  392.         InitCursor;
  393.         if FlushEvents(6, 0) <> 6 then begin end;
  394.     END;
  395.     
  396. {******************************************************************************
  397. *
  398. * closeTools:   Shut down the tools I started.
  399. *
  400. * Inputs:       NONE
  401. * Outputs:      NONE
  402. * Calls:        NONE
  403. }
  404.  
  405. procedure closeTools;
  406.  
  407. begin
  408.     { shut down tools started by StartUpTools }
  409.     ShutDownTools(refIsHandle,ToolRecRef);
  410.     
  411.     { shut down Memory Manager and Tool Locator }
  412.     MMShutDown(MyMemoryID);
  413.     TLShutDown;
  414. end;
  415.  
  416.  
  417. {******************************************************************************
  418. *
  419. * closeApp:     Close down things. This disposes of all items and
  420. *               memory that we allocated. Usually undoes what was done
  421. *               in initApp.
  422. *
  423. * Inputs:       NONE
  424. * Outputs:      NONE
  425. * Calls:        NONE
  426. }
  427.  
  428. procedure closeApp;
  429.  
  430. begin
  431.     CloseWindow(FrontWindow);
  432. end;
  433.  
  434.  
  435. {******************************************************************************
  436. *
  437. * eventLoop:    Main Event Loop. Handle things until user selects Quit.
  438. *
  439. * Inputs:       NONE
  440. * Outputs:      NONE
  441. * Calls:        testTopWindow, doMenu
  442. }
  443.  
  444. procedure eventLoop;
  445.  
  446. var
  447.     taskCode    : integer;      { code indicating action to be taken }
  448.  
  449. begin
  450.     repeat
  451.         testTopWindow;          { test top window to see if it is a NDA }
  452.         
  453.         taskCode := TaskMaster(EveryEvent,MyEvent);
  454.         case taskCode of        { handle the event for this taskcode }
  455.             {   With most of these events, we do nothing (in fact, most
  456.                 applications will never see some of these events). You
  457.                 should cut the labels for events your application does
  458.                 not use out of this case statement. Any of these events
  459.                 your application does use should call a procedure to handle
  460.                 the event.  }
  461.             nullEvt:;
  462.             mouseDownEvt:;
  463.             mouseUpEvt:;
  464.             keyDownEvt:;
  465.             autoKeyEvt:;
  466.             updateEvt:;
  467.             activateEvt:;
  468.             switchEvt:;
  469.             deskAccEvt:;
  470.             driverEvt:;
  471.             app1Evt:;
  472.             app2Evt:;
  473.             app3Evt:;
  474.             app4Evt:;
  475.             wInDesk:;
  476.             wInMenuBar,             { do "In system menu bar" events and }
  477.             wInSpecial: doMenu;     { "Item ID selected was 250-255" events }
  478.             wClickCalled:;
  479.             wInContent:;
  480.             wInDrag:;
  481.             wInGrow:;
  482.             wInGoAway:;
  483.             wInZoom:;
  484.             wInInfo:;
  485.             wInDeskItem:;
  486.             wInFrame:;
  487.             wInactMenu:;
  488.             wClosedNDA:;
  489.             wCalledSysEdit:;
  490.             wTrackZoom:;
  491.             wHitFrame:;
  492.             wInControl: doInControl;
  493.             wInControlMenu:;
  494.         end;
  495.     until Done;                     { Loop until "Quit" is selected }
  496. end;
  497.  
  498. procedure OpenTheWindow;
  499.  
  500. begin
  501.     OurWindow := NewWindow2(NIL,0,@DrawThisWindow,NIL,2,ref(MainWindowID),
  502.                             rWindParam1);
  503.     ShowWindow(OurWindow);
  504.     SelectWindow(OurWindow);
  505.     hiliteDocBlk;
  506.     hiliteSndBlk;
  507.     hiliteParmX(1,'');
  508.     hiliteParmX(2,'');
  509.     hiliteParmX(3,'');
  510. end;
  511.  
  512.  
  513.  
  514.  
  515. {******************************************************************************
  516. *
  517. * initApp:      Perform any application specific initialization. For
  518. *               this app,  we do is initialize the Done to false,
  519. *               set WindowKind to an application window kind, initialize all
  520. *               of the menus and initialize the TaskMask in the event record.
  521. *               You might use this procedure to create windows,
  522. *               initialize variables and allocate memory needed for
  523. *               the entire program.
  524. *
  525. * Inputs:       NONE
  526. * Outputs:      NONE
  527. * Calls:        NONE
  528. }
  529.  
  530. procedure initApp;
  531.  
  532. begin
  533.     Done := false;              { we aren't done yet }
  534.     
  535.     WindowKind := 0;            { window kind  = application }
  536.     
  537.     { tell TaskMaster what events to handle }
  538.     MyEvent.wmTaskMask := MyTaskMask;
  539.     
  540.     { create default system menu bar from a resource
  541.       and make it the current menu bar }
  542.       
  543.     SetSysBar(NewMenuBar2(refIsResource,Ref(MenuBarOneRID),NIL));
  544.     SetMenuBar(NIL);
  545.     WaitCursor;
  546.  
  547.     InitSoundEx;    
  548.  
  549.     RefreshDeskTop(NIL);        { redraw the desktop }
  550.     
  551. {   InitCursor;                  normal arrow cursor }
  552.     
  553.     FixAppleMenu(AppleMenuID);  { add NDAs to Apple menu }
  554.     MenuHeight := FixMenuBar;   { set menu bar height }
  555.     DrawMenuBar;                { draw the menu bar }
  556.     OpenTheWindow;              { Open our one and only window }
  557.     InitCursor;
  558.     if FlushEvents(6, 0) <> 6 then begin end;
  559. end;
  560.  
  561.  
  562. {******************************************************************************
  563. *
  564. * errorCheck:   This routine is called by initTools to check for startup
  565. *               errors. An error message is shown and everything is
  566. *               shut down if any errors are detected.
  567. *
  568. * Inputs:       where = the a reference number that tells you where in the
  569. *               initTools procedure the error happened.
  570. * Outputs:      NONE (program exits)
  571. * Calls:        closeTools
  572. }
  573.  
  574. procedure errorCheck(where : Integer);
  575.  
  576. var
  577.     theError    : integer;      { the tool error number }
  578.     errStr      : str255;       { string to display error message }
  579.     tempChar    : integer;      { temp to eat character returned }
  580.  
  581. begin
  582.     if _toolErr <> 0 { _toolErr is an external var }
  583.         then
  584.             begin
  585.                 theError := _toolErr;   { store the error number }
  586.                 
  587.                 { initialize errStr }
  588.                 errStr := 
  589.             'Fatal Error $0000 has occurred at xxxx. Press any key to exit:';
  590.     
  591.                 { Stick error # into a string }
  592.                 Int2Hex(theError,Pointer(Ord4(@errStr)+14),4);
  593.  
  594.                 { Stick loc # into a string }
  595.                 Int2Hex(where,Pointer(Ord4(@errStr)+35),4);
  596.     
  597.                 GrafOff;                        { turn off super Hires }
  598.                 WriteLine(errStr);              { write errStr to text screen }
  599.                 SysBeep;                        { ring the bell }
  600.                 tempChar := ReadChar(noEcho);   { & wait for keypress }
  601.     
  602.                 closeTools;     { ShutDown my Tools }
  603.                 Halt;           { quit with APW status = 1, MPW Pascal call! }
  604.             end;
  605. end;
  606.  
  607.  
  608. {******************************************************************************
  609. *
  610. * initTools:    Load and startup the tools needed. errorCheck is called
  611. *               after each startup to check for errors.
  612. *
  613. * Inputs:       NONE
  614. * Outputs:      NONE
  615. * Calls:        errorCheck
  616. }
  617.  
  618. procedure initTools;
  619.  
  620. begin
  621.     TLStartUp;                  { start up Tool Locator }
  622.     errorCheck(1);              { Make sure all is OK }
  623.  
  624.     MyMemoryID := MMStartUp;    { start up Memory Manager & get Memory ID }
  625.     errorCheck(2);              { Make sure all is OK }
  626.     
  627.     { start up the rest of the tools }
  628.     ToolRecRef := StartUpTools(MyMemoryID,refIsResource,Ref(BaseResID+1));
  629.     errorCheck(3);              { Make sure all is OK }
  630. end;
  631.  
  632.  
  633. {******************************************************************************
  634. *
  635. * main:         This is the main routine. It calls procedures to startup
  636. *               the tools, initialize application specific data, run the
  637. *               main eventLoop, close the application, and shutdown the tools.
  638. *               
  639. * Inputs:       NONE
  640. * Outputs:      NONE
  641. * Calls:        initTools, initApp, eventLoop, closeApp, closeTools
  642. }
  643.  
  644. begin
  645.     initTools;              { Initialize tools. }
  646.     initApp;                { Initialize application specific stuff. }
  647.  
  648.     eventLoop;              { Do application stuff until user wants to
  649.                               do something else! }
  650.  
  651.     closeApp;               { ShutDown application specific things. }
  652.     closeTools;             { ShutDown the tools. }
  653. end.
  654.